<!DOCTYPE html>
<html lang="en">

<head>
    <title>UserModifyRole</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/html/main/config/lib/main.css" media="all">
</head>

<body>
    <div class="layui-form-item">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">选择角色：</label>
                <div class="layui-input-block spec">
                </div>
            </div>
            <div class="layui-form-item layui-hide">
                <button class="layui-btn" lay-submit lay-filter="YjbtnSave" id="YjbtnSave" type="button">保存</button>
            </div>
        </form>
    </div>
    <script src="/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/src/'
        }).use('index', function() {
            var table = layui.table,
                form = layui.form,
                main = layui.main,
                setter = layui.setter,
                dropdown = layui.dropdown,
                laydate = layui.laydate,
                $ = layui.jquery,
                admin = layui.admin;

            function getQueryString(name) {
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
                var r = window.location.search.substring(1).match(reg);
                if (r != null) {
                    return decodeURI(r[2]);
                }
                return "0";
            }
            var userid = getQueryString("uid");

            //加载所有角色
            parent.layui.admin.req({
                url: 'system/role/Query',
                type: 'get',
                data: {
                    limit: 0
                },
                done: function(res) {
                    var html = "";
                    //获取用户角色
                    parent.layui.admin.req({
                        url: 'system/userinrole/Query',
                        data: {
                            userid: userid,
                            page: 0
                        },
                        type: 'get',
                        done: function(resuser) {
                            if (resuser.count > 0) {
                                var ridstr = ",";
                                $.each(resuser.data, function(i, t) {
                                    ridstr += t.roleid + ",";
                                })
                                $.each(res.data, function(index, item) {
                                    if (ridstr.indexOf("," + item.id + ",") > -1) {
                                        html += '<input id="roleid" type="checkbox" name="spec_ids[]" title="' + item.rolename + '"  checked="true" lay-skin="primary" value="' + item.id + '">';
                                    } else {
                                        html += '<input id="roleid" type="checkbox" name="spec_ids[]" title="' + item.rolename + '"  lay-skin="primary" value="' + item.id + '">';
                                    }
                                });
                            } else {
                                $.each(res.data, function(index, item) {
                                    html += '<input id="roleid" type="checkbox" name="spec_ids[]" title="' + item.rolename + '"  lay-skin="primary" value="' + item.id + '">';
                                });
                            }
                            $('.spec').prepend(html);
                            form.render();
                        }
                    });
                }
            });

            //保存
            form.on('submit(YjbtnSave)', function(data) {
                var rid = GetCheckboxValues("roleid");
                parent.layui.admin.req({
                    url: 'system/userinrole/UserRoleModify',
                    data: {
                        uid: userid,
                        rid: rid
                    },
                    type: 'get',
                    done: function(res) {
                        parent.layer.msg(res.msg);
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }
                });
            });

            function GetCheckboxValues(Name) {
                var result = [];
                $("[id='" + Name + "']:checkbox").each(function() {
                    if ($(this).is(":checked")) {
                        result.push($(this).attr("value"));
                    }
                });
                return result.join(",");
            };

        });
    </script>
</body>

</html>